import StarMaskOnboarding from '@starcoin/starmask-onboarding'
import { providers, utils, bcs, encoding } from '@starcoin/starcoin'

let provider = new providers.JsonRpcProvider("http://localhost:9850");
let starcoinProvider;
let accounts;
export const initialize = async function (){
    console.log('页面初始化....')
    if (window.starcoin == null){
        throw "NULL";
    }
    try {
        if (window.starcoin) {
            // We must specify the network as 'any' for starcoin to allow network changes
            starcoinProvider = new providers.Web3Provider(window.starcoin, 'any')
        }
    } catch (error) {
        console.error(error)
    }

    let onboarding
    try {
        onboarding = new StarMaskOnboarding({ undefined })
    } catch (error) {
        console.error(error)
    }
}
export const connectWallet = async  function(){
    try {
        const newAccounts = await window.starcoin.request({
            method: 'stc_requestAccounts',
        })
        accounts = newAccounts;
        return  accounts;
    } catch (error) {
        throw error;
    }
}
export   const isStarMaskConnected = function(){
  return   accounts && accounts.length > 0;
}